/*
 *  Android Weather Notification.
 *  Copyright (C) 2010  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 */

package ru.gelin.android.weather.notification.app;

import android.app.ActionBar;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.widget.CompoundButton;
import android.widget.Switch;
import ru.gelin.android.weather.notification.R;
import ru.gelin.android.weather.notification.skin.SkinInfo;

import java.util.List;

import static ru.gelin.android.weather.notification.PreferenceKeys.ENABLE_NOTIFICATION;
import static ru.gelin.android.weather.notification.PreferenceKeys.ENABLE_NOTIFICATION_DEFAULT;
import static ru.gelin.android.weather.notification.app.PreferenceKeys.REFRESH_INTERVAL;
import static ru.gelin.android.weather.notification.app.PreferenceKeys.SKINS_CATEGORY;

/**
 *  Main activity for ICS and later Androids.
 */
public class MainActivity4 extends BaseMainActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //http://xgouchet.fr/android/index.php?article4/master-on-off-preferences-with-ice-cream-sandwich
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.global_switch);

        Switch actionBarSwitch = (Switch)actionBar.getCustomView();
        boolean notificationEnabled = PreferenceManager.getDefaultSharedPreferences(this).
                getBoolean(ENABLE_NOTIFICATION, ENABLE_NOTIFICATION_DEFAULT);
        actionBarSwitch.setChecked(notificationEnabled);
        findPreference(REFRESH_INTERVAL).setEnabled(notificationEnabled);
        actionBarSwitch.setOnCheckedChangeListener(this);
    }

    protected void fillSkinsPreferences(List<SkinInfo> skins) {
        PreferenceCategory skinsCategory = (PreferenceCategory)findPreference(SKINS_CATEGORY);
        for (SkinInfo skin : skins) {
            SwitchPreference switchPref = skin.getSwitchPreference(this);
            switchPref.setOnPreferenceChangeListener(this);
            skinsCategory.addPreference(switchPref);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putBoolean(ENABLE_NOTIFICATION, isChecked).commit();
        startUpdate(false);
        findPreference(REFRESH_INTERVAL).setEnabled(isChecked);
    }

    @Override
    protected void startProgress() {
        getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
        super.startProgress();
    }

    @Override
    protected void stopProgress() {
        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
        super.stopProgress();
    }

}